package com.scan.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.bean.BaseController;
import com.bean.BaseResult;
import com.scan.dao.model.User;
import com.scan.dao.model.VoteActiver;
import com.scan.dao.model.VoteSubject;
import com.scan.service.VoteActiverService;
import com.scan.service.VoteSubjectService;

import static com.bean.Cons.SESSION_USER;

/**
 * Created by tancw on 2017/2/8.
 */
@RestController
@RequestMapping("vote")
public class VoteController extends BaseController{

    @Autowired
    private VoteSubjectService voteSubjectService;

    @Autowired
    private VoteActiverService voteActiverService;

    @RequestMapping(value = "subject", method = RequestMethod.GET)
    public VoteSubject list(@RequestParam(value = "voteId", required = true) Integer id) {
        return voteSubjectService.getSubjectById(id);
    }

    @RequestMapping(value = "doActive.do", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult vote(HttpServletRequest request, VoteActiver activer) {
        User user = (User) request.getSession().getAttribute(SESSION_USER);
        activer.setIp(getIp(request));
        activer.setSubtime(new Date());
        int result = voteActiverService.create(activer);
        return new BaseResult(result > 0, "保存");
    }
}
